package com.fruitshop.controller;

import com.fruitshop.dto.CartDTO;
import com.fruitshop.service.CartService;
import com.fruitshop.vo.CartVO;
import com.fruitshop.vo.ResultVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车控制器
 */
@Slf4j
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {
    
    private final CartService cartService;
    
    /**
     * 添加购物车
     *
     * @param request HTTP请求
     * @param cartDTO 购物车数据
     * @return 添加结果
     */
    @PostMapping("/add")
    public ResultVO<Boolean> add(HttpServletRequest request, @RequestBody @Valid CartDTO cartDTO) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.add(userId, cartDTO);
        return ResultVO.success(result);
    }
    
    /**
     * 修改购物车
     *
     * @param request HTTP请求
     * @param id      购物车ID
     * @param cartDTO 购物车数据
     * @return 修改结果
     */
    @PutMapping("/{id}")
    public ResultVO<Boolean> update(HttpServletRequest request, @PathVariable Long id, @RequestBody @Valid CartDTO cartDTO) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.update(userId, id, cartDTO);
        return ResultVO.success(result);
    }
    
    /**
     * 删除购物车
     *
     * @param request HTTP请求
     * @param id      购物车ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResultVO<Boolean> delete(HttpServletRequest request, @PathVariable Long id) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.delete(userId, id);
        return ResultVO.success(result);
    }
    
    /**
     * 清空购物车
     *
     * @param request HTTP请求
     * @return 清空结果
     */
    @DeleteMapping("/clear")
    public ResultVO<Boolean> clear(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.clear(userId);
        return ResultVO.success(result);
    }
    
    /**
     * 获取购物车列表
     *
     * @param request HTTP请求
     * @return 购物车列表
     */
    @GetMapping("/list")
    public ResultVO<List<CartVO>> list(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        List<CartVO> cartList = cartService.list(userId);
        return ResultVO.success(cartList);
    }
    
    /**
     * 更新购物车选中状态
     *
     * @param request  HTTP请求
     * @param id       购物车ID
     * @param selected 选中状态
     * @return 更新结果
     */
    @PutMapping("/selected/{id}")
    public ResultVO<Boolean> updateSelected(HttpServletRequest request, @PathVariable Long id, @RequestParam Integer selected) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.updateSelected(userId, id, selected);
        return ResultVO.success(result);
    }
    
    /**
     * 全选/取消全选
     *
     * @param request  HTTP请求
     * @param selected 选中状态
     * @return 更新结果
     */
    @PutMapping("/select-all")
    public ResultVO<Boolean> selectAll(HttpServletRequest request, @RequestParam Integer selected) {
        Long userId = (Long) request.getAttribute("userId");
        Boolean result = cartService.selectAll(userId, selected);
        return ResultVO.success(result);
    }
    
    /**
     * 获取购物车商品数量
     *
     * @param request HTTP请求
     * @return 商品数量
     */
    @GetMapping("/count")
    public ResultVO<Integer> count(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        Integer count = cartService.count(userId);
        return ResultVO.success(count);
    }
    
    /**
     * 获取购物车总金额
     *
     * @param request HTTP请求
     * @return 总金额
     */
    @GetMapping("/total-amount")
    public ResultVO<BigDecimal> getTotalAmount(HttpServletRequest request) {
        Long userId = (Long) request.getAttribute("userId");
        BigDecimal totalAmount = cartService.getTotalAmount(userId);
        return ResultVO.success(totalAmount);
    }
} 